package com.c2b.algorithm.leetcode.base;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <a href="https://leetcode.cn/problems/binary-tree-right-side-view/">二叉树的右视图(Binary Tree Right Side View)</a>
 * <p>给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。</p>
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1:
 *      输入: [1,2,3,null,5,null,4]
 *                  1       <=
 *                 / \
 *                2   3     <=
 *                 \   \
 *                  5   4   <=
 *      输出: [1,3,4]
 *
 * 示例 2:
 *      输入: [1,null,3]
 *      输出: [1,3]
 *
 * 示例 3:
 *      输入: []
 *      输出: []
 * </pre>
 * </p>
 * <p>
 * <b>提示:</b>
 * <ul>
 *     <li>二叉树的节点个数的范围是 [0,100]</li>
 *     <li>-100 <= Node.val <= 100 </li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/5/12 10:52
 */
public class LC0199BinaryTreeRightSideView_M {

    static class Solution {

        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> retList = new ArrayList<>();
            if (root == null) {
                return retList;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                // 一次处理一层的节点
                for (int i = 0; i < size; i++) {
                    TreeNode currNode = queue.poll();
                    if (i == size - 1) {
                        retList.add(currNode.val);
                    }
                    if (currNode.left != null) {
                        queue.offer(currNode.left);
                    }
                    if (currNode.right != null) {
                        queue.offer(currNode.right);
                    }
                }
            }
            return retList;
        }

        public List<Integer> rightSideView2(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            TreeNode currLevelLastNode = root;
            TreeNode nextLevelLastNode = null;
            while (!queue.isEmpty()) {
                TreeNode currNode = queue.poll();
                if (currNode.left != null) {
                    queue.offer(currNode.left);
                    nextLevelLastNode = currNode.left;
                }
                if (currNode.right != null) {
                    queue.offer(currNode.right);
                    nextLevelLastNode = currNode.right;
                }
                if (currLevelLastNode == currNode) {
                    res.add(currNode.val);
                    currLevelLastNode = nextLevelLastNode;
                    nextLevelLastNode = null;
                }
            }
            return res;
        }
    }


    public static void main(String[] args) {
        Solution solution = new Solution();

        //TreeNode root = new TreeNode(1);
        //root.left = new TreeNode(2);
        //root.right = new TreeNode(3);
        //root.left.right = new TreeNode(5);
        //root.right.right = new TreeNode(4);

        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(3);
        TreeNode.printTreeNodeValF(solution.rightSideView(root));
    }
}
